package com.yz.crm.phase.action;

import com.yz.common.model.Pager;
import com.yz.common.model.RemoteInteractObject;
import com.yz.crm.db.model.BaseCompany;
import com.yz.crm.db.model.BasePhase;
import com.yz.crm.passport.constant.PassportConst;
import com.yz.crm.passport.model.SessionUser;
import com.yz.crm.phase.constant.PhaseConstant;
import com.yz.crm.phase.service.PhaseService;
import com.yz.util.WebUtils;

import org.apache.commons.lang3.StringUtils;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpSession;

import java.util.HashMap;
import java.util.Map;

/**
 * Created by DOVE on 2017/5/18 0018.
 */
@Controller
@RequestMapping("/admin/phase")
public class PhaseAction {
    private static final Log log = LogFactory.getLog(PhaseAction.class);

    @Resource
    private PhaseService phaseService;

    @RequestMapping("/index")
    public String index() {
        return "/admin/phase/index";
    }

    @RequestMapping("/list")
    public String list(Pager pager, ModelMap modelMap, HttpServletRequest request) {
        SessionUser user = WebUtils.getLoginUser(request);
        if (user == null) {
            return null;
        }
        Map<String, Object> paramMap = new HashMap<String, Object>();
        paramMap.put(PhaseConstant.PAGER_KEY_COMPANY_ID, user.getCompany().getId());
        pager.setKeys(paramMap);
        modelMap.addAttribute("pager", phaseService.findPager(pager));
        return "/admin/phase/list";
    }

    @RequestMapping("/add")
    public String add() {
        return "/admin/phase/add";
    }

    @RequestMapping("/save")
    @ResponseBody
    public RemoteInteractObject save(HttpServletRequest request, String name) {
        RemoteInteractObject result = new RemoteInteractObject();

        SessionUser user = WebUtils.getLoginUser(request);
        if (user == null) {
            result.setSuccess(false);
            result.setMsg("查询公司信息出错，请联系管理员");
            return result;
        }
        BasePhase phase = new BasePhase();
        phase.setName(name);
        phase.setCompanyId(user.getCompany().getId());
        phase.setCreatorName(user.getCompany().getName());
        phase.setModifierName(user.getCompany().getName());
        phaseService.add(phase);
        result.setSuccess(true);
        return result;
    }

}
